package com.example.mcdonaldapplication.info

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mcdonaldapplication.R
import com.example.mcdonaldapplication.activity.TakeawayActivity


class AddressAdapter(private val mutableList: List<String>, private val username:String, private val phone:String, private val context:Activity)  : RecyclerView.Adapter<AddressAdapter.ViewHolder>() {

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val tvUsername:TextView = itemView.findViewById(R.id.tvUsername)
        val tvPhone:TextView = itemView.findViewById(R.id.tvPhone)
        val tvAddress:TextView = itemView.findViewById(R.id.tvAddress)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.address_item,parent,false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = mutableList[position]
        holder.tvAddress.text = data
        holder.tvPhone.text = phone
        holder.tvUsername.text = username
        holder.itemView.setOnClickListener{
            val intent = Intent(context, TakeawayActivity::class.java)
            intent.putExtra("address", data)
            context.finish()
            context.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return mutableList.size
    }
}
